#region Summary

// 
// Owner : JackieHan 
// 
// Solution : WebShopApi 
// 
// Project : Jackie.Utility
// 
// File : IgnoredNullDictionary.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 忽略空值的Dictionary
// 
// History : 
//			2011-03-15 15:32:48	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;

#endregion

namespace Jackie.Utility.CostomCollection
{
    /// <summary> 
    /// 忽略空值的全字符串Dictionary
    /// </summary>
    public class IgnoredNullStringDictionary : Dictionary<string, string>
    {

        private const string DateTimeFormat = "yyyy-MM-dd HH:mm:ss";

        public IgnoredNullStringDictionary()
        {
        }

        public IgnoredNullStringDictionary(IDictionary<string, string> dictionary)
            : base(dictionary)
        {
        }

        /// <summary>
        /// 添加一对数据到集合  对Bool值特殊处理
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">boolean值</param>
        /// <param name="isOneOrZero">是否需要转换成01类型</param>
        public void Add(string key, bool? value, bool isOneOrZero)
        {
            if (value.HasValue)
            {
                var strValue = isOneOrZero ? (value.Value ? "1" : "0") : value.Value.ToString();
                this.Add(key, strValue);
            }
        }

        /// <summary>
        /// 添加一对数据到集合
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值：支持string,DateTime,int ,long,double,bool,decimal及其可空类型和枚举（转换成int）</param>
        public void Add(string key, object value)
        {
            string strValue;
            if (value == null)
                strValue = null;
            else if (value is string)
                strValue = (string)value;
            else if (value is DateTime?)
                strValue = (value as DateTime?).Value.ToString(DateTimeFormat);
            else if (value is int?)
                strValue = (value as int?).Value.ToString();
            else if (value is long?)
                strValue = (value as long?).Value.ToString();
            else if (value is double?)
                strValue = (value as double?).Value.ToString();
            else if (value is bool?)
                strValue = (value as bool?).Value.ToString();
            else if (value is decimal?)
                strValue = (value as decimal?).Value.ToString();
            else if (value is Enum)
                strValue = ((int)value).ToString();
            else
                strValue = value.ToString();

            this.Add(key, strValue);
        }

        public new void Add(string key, string value)
        {
            if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
                base.Add(key, value);
        }
    }
}